package edu.zsl.service.impl;

import edu.zsl.mapper.ScoreMapper;
import edu.zsl.pojo.Score;
import edu.zsl.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ScoreServiceImpl implements ScoreService {
    private final ScoreMapper scoreMapper;
    @Autowired
    public ScoreServiceImpl(ScoreMapper scoreMapper) {
        this.scoreMapper = scoreMapper;
    }

    //根据学号查询成绩并带上学生名称和课程名称
    @Override
    public List<Score> searchScoreWithName(String studentId){
        return scoreMapper.getAllWithName(studentId);
    }

    //查询全部人的全部成绩
    @Override
    public List<Score> getAllScores(){
        return scoreMapper.getAll();
    }

    //新增学生成绩
    @Override
    public void addScore(Score score) {
        LocalDateTime now = LocalDateTime.now();
        score.setCreateBy(1);
        score.setCreateTime(now);
        scoreMapper.insert(score);
    }

    //删除学生成绩
    @Override
    public void deleteScore(Integer id){
        scoreMapper.deleteById(id);
    }

    //根据id得到学生成绩
    @Override
    public Score getScoreById(Integer id){
        Score score = scoreMapper.getById(id);
        return score;
    }

    //    更新学生成绩
    @Override
    public void updateScore(Score score){
        LocalDateTime now = LocalDateTime.now();
        score.setUpdateBy(1);
        score.setUpdateTime(now);
        scoreMapper.update(score);
    }

    //    显示学生所有课程成绩的平均分
    @Override
    public Double getAvgScore(String studentId){
        return scoreMapper.GetAvgScore(studentId);
    }
}
